<?php

namespace app\admin\controller\prl;

use app\common\controller\Backend;
use app\admin\model\prl\Channels as ChannelsModel;
use app\admin\model\prl\Articles as ArticlesModel;
use think\Db;
use fast\Form;
/**
 * 模型管理
 *
 * @icon fa fa-circle-o
 */
class Models extends Backend
{

    protected $noNeedRight = ['getModelField'];
    
    /**
     * Models模型对象
     * @var \app\admin\model\prl\Models
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\prl\Models;

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                $row->visible(['id','name','table']);
                
            }
            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function getModelField()
    {
        $channel_ids = $this->request->param('channel_ids/a'); //栏目id
        $row_id = $this->request->param('row_id'); //文章id
        $model_ids = ChannelsModel::where('id','in', $channel_ids)->column('models_id');
        $model_list = $this->model->where('id', 'in', $model_ids)->select();

        $fields = [];
        foreach ($model_list as $model) {
            //获取文章模型数据
            if(!empty($row_id)){
                $table_value = Db::table($model->table)->where('id', $row_id)->find();
            }
            foreach ($model->modelsfields as $field) {
                if(isset($table_value[$field->name])){//如果有值
                    $field->value = $table_value[$field->name];
                }
                $fields[$model->table][] = $field->toArray();
            }
        }
        $html = '';
        if(!empty($fields)){
            foreach ($fields as $table => $fields) {
                $html .= Form::hidden('row[models][' . $table . '][id]',$row_id);
                foreach ($fields as $field) {
                    $name = 'row[models][' . $table . '][' . $field['name'] . ']';
                    $title = $field['title'];
                    if (in_array($field['type'], ['select', 'selects', 'checkbox', 'radio'])) {
                        $field['content'] = json_decode($field['content'], TRUE);
                    }
                    $field['rule'] = empty($field['rule']) ? [] : $field['rule'];
                    $html .= '<div class="form-group">';
                    $html .= Form::label($name, $title, ['class' => 'control-label col-xs-12 col-sm-2']);
                    $html .= '<div class="col-xs-12 col-sm-8">';
                    switch ($field['type']) {
                        case 'text' :
                            $html .= Form::text($name, $field['value'], $field['rule']);
                            break;
                        case 'editor' :
                            $html .= Form::editor($name, $field['value'], $field['rule']);
                            break;
                        case 'date' :
                            $html .= Form::datepicker($name, $field['value'], $field['rule']);
                            break;
                        case 'time' :
                            $html .= Form::timepicker($name, $field['value'], $field['rule']);
                            break;
                        case 'datetime' :
                            $html .= Form::datetimepicker($name, $field['value'], $field['rule']);
                            break;
                        case 'select':
                            $html .= Form::selectpicker($name, $field['content'], $field['value'], $field['rule']);
                            break;
                        case 'selects':
                            $html .= Form::selectpickers($name, $field['content'], $field['value'], $field['rule']);
                            break;
                        case 'image':
                            $html .= Form::image($name, $field['value']);
                            break;
                        case 'images' :
                            $html .= Form::images($name, $field['value']);
                            break;
                        case 'file' :
                            $html .= Form::upload($name, $field['value']);
                            break;
                        case 'files' :
                            $html .= Form::uploads($name, $field['value']);
                            break;
                        case 'switch' :
                            $html .= Form::switcher($name, $field['value'], $field['rule']);
                            break;
                        case 'checkbox' :
                            //循环字典数据
                            foreach ($field['content'] as $k => $v) {
                                $html .= Form::checkbox($name, $k, in_array($k, $field['value']), $field['rule']);
                            }
                            break;
                        case 'radio' :
                            //循环字典数据
                            foreach ($field['content'] as $k => $v) {
                                $html .= Form::radio($name, $k, in_array($k, $field['value']), $field['rule']);
                            }
                            break;
                        default:
                            $html .= Form::input($field['type'], $name, $field['value'], $field['rule']);
                            break;
                    }
                    $html .= "</div></div>";
                }
            }
        }
        return $this->success('', '', $html);
    }
}
